• Jump To … +
    main.js separate.js single.js web-apg-api.js main.js web-conv-api.js ast.js csv.js dangling-else.js display.js flags.js float.js limits.js main.js multiline-mode.js recursive.js replace.js rules.js split.js testonly.js trace.js udt.js unicode.js web-email.js word-boundaries.js main.js phone-number.js web-main.js web-phone-number.js main.js phone-number.js setup.js translate.js xml.js branch-fail-grammar.js main.js parent-mode-grammar.js setup.js universal-mode-grammar.js colors-app.js colors-callbacks.js colors.js main.js more-app.js more-setup.js more.js ast-callbacks.js bad-input.js basic.js ini-file.js main.js parser-callbacks.js setup.js trace.js anbncn.js and.js c-comment.js compound.js main.js nested.js not.js setup.js boundaries-grammar.js boundaries.js comment-grammar.js comment.js main.js negative-grammar.js negative.js positive-grammar.js positive.js setup.js main.js odata-grammar.js run.js setup.js area-code.js lookaround.js main.js phone-number.js setup.js simple.js all-operators.js default.js fancy-number.js limited-lines.js main.js select-operators.js select-rules.js setup.js main.js minimal.js parent-u.js parent.js phone-number.js setup.js stats.js trace.js universal-u.js universal.js callbacks.js grammar.js main.js parser.js writeHtml.js LICENSE.md README.md index.md
  • display.js

  • §
    /* eslint-disable new-cap */
    /*  *************************************************************************************
     *   copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
     *     license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
     *   ********************************************************************************* */
  • §

    This module demonstrates some of the many options available for displaying the results. Functions are available for displaying the source (grammar syntax), the result and the last match (the apg-exp object). The last result is patterned after the JavaScript RegExp object as described at MDN

    Let:

    let apgExp = require("apg-exp");
    let grammar = 'rule = "abc"\n';
    let exp = new apgExp(grammar);
    let str = "<<<-abc->>>";
    let result = exp.exec(str);
    

    There are text and HTML display options for each of the objects result, exp and exp.source.

    • `result`:
      • `result.toText()` - returns a text string of the results.
      • `result.toHtml()` - returns the results in HTML format.
      • `result.toHtmlPage()` - returns the results in a complete HTML page, ready for browser viewing.
    • `exp`:
      • `exp.toText()` - returns the `last match` as a text string.
      • `exp.toHtml()` - returns the `last match` in HTML format.
      • `exp.toHtmlPage()` - returns the `last match` in a complete HTML page, ready for browser viewing.
    • `source` (SABNF grammar):
      • `exp.sourceToText()` - returns source as a text string.
      • `exp.sourceToHtml()` - returns source in HTML format.
      • `exp.sourceToHtmlPage()` - returns source in a complete HTML page, ready for browser viewing.
    (function displaying() {
      try {
        const apgJs = require('apg-js');
        const writeHtml = require('../writeHtml');
    
        const { apgExp } = apgJs;
        const { apgLib } = apgJs;
        const grammar = 'pattern = "abc"\n';
        let exp;
        let flags;
        let result;
        let html;
        let txt;
        let page;
        html = '';
        flags = '';
        const str = '<<<-abc->>>';
        exp = new apgExp(grammar, flags);
        result = exp.exec(str);
        txt = exp.sourceToText();
    
        /* source */
  • §

    There are text and HTML display options for each of the objects result, exp and exp.source.

        console.log();
        console.log('Display demonstration:');
        console.log('There are text and HTML display options for each of the objects `result`, `exp` and `exp.source`');
        console.log();
        console.log('source:');
        console.log(txt);
        html += '<h3>source example</h3>';
        html += exp.sourceToHtml();
        page = exp.sourceToHtmlPage();
        writeHtml(page, 'display-source');
    
        /* results */
        txt = result.toText();
        console.log();
        console.log(txt);
        html += '<h3>results example</h3>';
        html += result.toHtml();
        page = result.toHtmlPage();
        writeHtml(page, 'display-results');
    
        /* last match */
        txt = exp.toText();
        console.log();
        console.log(txt);
        html += '<h3>last match example</h3>';
        html += exp.toHtml();
        page = exp.toHtmlPage();
        writeHtml(page, 'display-exp');
    
        html = apgLib.utils.htmlToPage(html);
        writeHtml(html, 'display-all');
  • §

    With the unicode flag, u, result and last match each has four mode options, ascii, decimal, hexidecimal and unicode. These are demonstrated here for the last match.

        flags = 'u';
        exp = new apgExp(grammar, flags);
        result = exp.exec(str);
        html = '<h3>last match ASCII</h3>';
        html += exp.toHtml('ascii');
        html += '<h3>last match decimal</h3>';
        html += exp.toHtml('decimal');
        html += '<h3>last match hexidecimal</h3>';
        html += exp.toHtml('hexidecimal');
        html += '<h3>last match Unicode</h3>';
        html += exp.toHtml('Unicode');
        html += '<h3>result Unicode</h3>';
        html += result.toHtml('Unicode');
        html = apgLib.utils.htmlToPage(html);
        writeHtml(html, 'display-modes');
      } catch (e) {
        console.log(`EXCEPTION: ${e.message}`);
      }
    })();